package com.arcbbs.controller;

import com.arcbbs.entity.FormBoard;
import com.arcbbs.mapper.FormBoardMapper;
import com.arcbbs.result.PageResult;
import com.arcbbs.result.Result;
import com.arcbbs.service.FormBoardService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/board")
@RestController
public class BoardController {
    @Resource
    private FormBoardService formBoardService;
    @Resource
    private FormBoardMapper formBoardMapper;
    @RequestMapping("/list")
    public Result list(Integer pageNo){
        PageResult pageResult=formBoardService.selectbypage(pageNo);
        return Result.success(pageResult);
    }
    @RequestMapping("/all")
    public Result all(){
        List<FormBoard> list=formBoardMapper.selectbypid();
        return Result.success(list);
    }
    @RequestMapping("/addandupdate")
    public Result addandupdate(Integer boardId,Integer pboardId,String boardName){
        if(boardId!=null){
            FormBoard formBoard=formBoardMapper.selectbyid(boardId);
            formBoard.setPBoardId(pboardId);
            formBoard.setBoardName(boardName);
        formBoardMapper.update(formBoard);
        }else{
            FormBoard formBoard=new FormBoard();
            formBoard.setBoardName(boardName);
            formBoard.setPBoardId(pboardId);
            formBoardMapper.insert(formBoard);
        }
        return Result.success();
    }
    @RequestMapping("/delete")
    public Result delete(Integer boardId){
        formBoardMapper.delete(boardId);
        return Result.success();
    }
}
